package com.jcfk.eam.service.main;

import com.alibaba.druid.util.StringUtils;
import com.jcfk.eam.domain.dto.main.EamTaskPlanEquDTO;
import com.jcfk.eam.domain.po.main.EamInspectionPlanTeam;
import com.jcfk.eam.domain.po.main.EamTaskPlanEqu;
import com.jcfk.eam.domain.query.main.EamInspectionPlanTeamQuery;
import com.jcfk.eam.domain.dto.main.EamInspectionPlanTeamDTO;
import com.jcfk.eam.dao.main.EamInspectionPlanTeamDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;

import java.util.ArrayList;
import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 巡检计划负责班组
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-09-24
 */
@Service
public class EamInspectionPlanTeamService extends BaseService<EamInspectionPlanTeamDao, EamInspectionPlanTeam> {

    /**
     * EamInspectionPlanTeam分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamInspectionPlanTeamDTO> queryByPage(DataPage<EamInspectionPlanTeamDTO> page, EamInspectionPlanTeamQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamInspectionPlanTeam是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamInspectionPlanTeamDTO dto) {
        QueryWrapper<EamInspectionPlanTeam> queryWrapper = new QueryWrapper<>();

        return RetResult.ok();
    }

    /**
     * 插入巡检计划负责班组
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamInspectionPlanTeamDTO dto) {

        // 保存
        EamInspectionPlanTeam item = new EamInspectionPlanTeam();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新巡检计划负责班组
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamInspectionPlanTeamDTO dto) {

        // 保存
        EamInspectionPlanTeam item = new EamInspectionPlanTeam();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }


    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamInspectionPlanTeam item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取巡检计划负责班组信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamInspectionPlanTeamDTO get(String id) {
        // 查询数据
        EamInspectionPlanTeam item = this.baseMapper.selectById(id);
        EamInspectionPlanTeamDTO dto = new EamInspectionPlanTeamDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamInspectionPlanTeamDTO>
     */
    public List<EamInspectionPlanTeamDTO> getList(EamInspectionPlanTeamQuery query) {
        // 查询条件
        QueryWrapper<EamInspectionPlanTeam> qw = new QueryWrapper<>();
        // 查询数据
        List<EamInspectionPlanTeam> list = super.list(qw);

        return convertList(list, EamInspectionPlanTeamDTO.class);
    }

    /**
     * 根据主键获取巡检计划负责班组信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        this.removeById(id);
    }


    /**
     * 作业计划设备批量保存
     *
     * @param dtos
     * @return StandardCodeDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(List<EamInspectionPlanTeamDTO> dtos, String id) {
        List<EamInspectionPlanTeam> insertList = new ArrayList<>();
        List<EamInspectionPlanTeam> updateList = new ArrayList<>();
        List<String> notDeleteIds = new ArrayList<>();

        for (EamInspectionPlanTeamDTO dto : dtos) {
            EamInspectionPlanTeam item = new EamInspectionPlanTeam();
            // 设置关联主键

            BeanUtils.copyProperties(dto, item);
            item.setPlanId(id);
            this.setDefaultValue(item);

            if (!StringUtils.isEmpty(dto.getId())) {
                notDeleteIds.add(dto.getId());
                updateList.add(item);
            } else {
                insertList.add(item);
            }
        }

        // 删除
        QueryWrapper<EamInspectionPlanTeam> queryWrapper = new QueryWrapper<>();
        if (notDeleteIds.size() > 0) queryWrapper.notIn("id", notDeleteIds);
        queryWrapper.lambda().eq(EamInspectionPlanTeam::getPlanId, id);
        this.remove(queryWrapper);

        // 保存新增
        if (insertList.size() > 0) this.saveBatch(insertList);

        // 保存更新
        if (updateList.size() > 0) this.updateBigBatch(updateList);
    }

}

